﻿using B.S.Meeting.Domain;
using B.S.Meeting.ErrorCode;
using B.S.Meeting.Infrastructure.Interfaces;
using B.S.Meeting.WriteAPI.Applications.Commands.Users;
using MediatR;

namespace B.S.Meeting.WriteAPI.Applications.CommandHandlers.Users
{
    public class DelUserCommandHandler : IRequestHandler<DelUserCommand, APIResult<int>>
    {
        IBaseRepository<User> uRep;
        ILogger<DelUserCommandHandler> logger;
        public DelUserCommandHandler(IBaseRepository<User> uRep, ILogger<DelUserCommandHandler> logger)
        {
            this.uRep = uRep;
            this.logger = logger;
        }
        public async Task<APIResult<int>> Handle(DelUserCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var user = await uRep.GetByIdAsync(request.UserId);
                if (user == null)
                {
                    return new APIResult<int>
                    {
                        Code = StatusEnumCode.Fail,
                        Message = "未找到指定的用户",
                        Data = 0
                    };
                }
                // 直接调用仓储层的删除方法进行物理删除
                await uRep.Del(request.UserId);
                logger.LogInformation($"成功删除用户: ID {request.UserId}");
                return new APIResult<int>
                {
                    Code = StatusEnumCode.Ok,
                    Message = "删除成功",
                    Data = 1
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
